﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KCMS.BusinessLogic.Interfaces;
using StructureMap;
using KCMS.BusinessLogic.Models;

namespace KCMS.Areas.Admin.Controllers
{
    public class OrderController : Controller
    {
        private IOrderService orderService = ObjectFactory.GetInstance<IOrderService>();
        private IOrderStatusService orderStatusService = ObjectFactory.GetInstance<IOrderStatusService>();
        private ICalculator calculator = ObjectFactory.GetInstance<ICalculator>();
        public ActionResult Index()
        {
            return View(orderService.getAll());
        }
        public ContentResult GetOrderStatus(Guid orderId)
        {
            OrderStatus status = orderStatusService.getLastByOrder(orderId);
            return Content(status.Title + " " + status.Date.ToShortDateString());
        }
        public ActionResult Details(Guid id)
        {
            return View(orderService.getById(id));
        }
        public ActionResult History(Guid id)
        {
            return View(orderStatusService.getAllByOrder(id));
        }
        [HttpGet]
        public ActionResult Delete(Guid id)
        {
            var model=orderService.getById(id);
            return View(model);
        }
        [HttpPost]
        public ActionResult Delete(Order model)
        {
            var order = orderService.getById(model.Id);
            IOrderItemService orderItemSevice = ObjectFactory.GetInstance<IOrderItemService>();
            var items = orderItemSevice.getByOrder(order.Id);
            foreach (var item in items)
            {
                orderItemSevice.delete(item.Id);
            }
            var status = orderStatusService.getAllByOrder(order.Id);
            foreach (var item in status)
            {
                orderStatusService.delete(item.Id);
            }
            orderService.delete(order.Id);
            return RedirectToAction("Index");
        }

    }
}
